package test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.swing.JOptionPane;

import algorithm.ToolInterface;

import comparators.AppointmentComparatorLength;

import entities.Appointment;
import entities.Calendar;
import entities.Category;
import fachlogik.Administration;
/**
public class GapFinderAC implements ToolInterface {

private Administration admin;

public GapFinderAC() {
    admin = admin.getInstance();
}

public void listToReschedule(List<Appointment> liste) {
    for (Appointment a : liste) {
        findGap(a);
    }
}

public void rescheduleAppointment(Date von, Date bis, Appointment appointmentToReschedule) {
    Appointment app = appointmentToReschedule;

    Date gap = findGap(app);
    if (gap != null)
    {
        long dauer = app.getLength();
        app.setBegin(gap);
        app.setEnd(new Date(gap.getTime() + dauer));
        SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm");
        JOptionPane
                .showMessageDialog(null, "Beginn: " + app.getBegin() + ", Ende: " + app.getEnd());
        return;
    }
    JOptionPane.showMessageDialog(null, "Diese Appointment konnte nicht gerescheduled werden");
}

public Date findGap(Appointment a) throws ParseException {

    Date von = admin.getRescheduleErlaubtVon(); // 20:00 //Begrenzung des
                                                // Zeitraums, in den
                                                // gereschedult werden darf.
    Date bis = admin.getRescheduleErlaubtBis(); // 21

    Category cat = a.getCategory();
    Date anfang = cat.getAnfangszeit(); // 10:10 //Begrenzung der Uhrzeit laut
                                        // Category des zu reschedulenden Apps.
    Date ende = cat.getEndzeit(); // 19:30
    boolean weErlaubt = cat.getWochenendeErlaubt();

    Calendar c = a.getCalendar();

    List<Appointment> appointmentliste = admin.appointmentsSortieren(admin // holen
                                                                           // aller
                                                                           // Apps
                                                                           // im
                                                                           // Reschedule-Zeitraum.
            .rescheduleBetweenTimespan(von, bis, c));

    if (appointmentliste != null) {
        int CatAHours = anfang.getHours(); // holen von hh der von-Begrenzung
                                           // der Category.
        int CatAMin = anfang.getMinutes(); // holen von MM der von-Begrenzung
                                           // der Category.
        int CatEHours = ende.getHours(); // holen von hh der bis-Begrenzung der
                                         // Category.
        int CatEMin = ende.getMinutes(); // holen von MM der bis-Begrenzung der
                                         // Category.
        List<Appointment> sorted = new ArrayList<>();
        for (Appointment app : appointmentliste) { // geht alle Apps im
                                                   // Reschedule-Zeitraum
                                                   // durch_______
            // if(weErlaubt)
            //
            // Beginn 16:00
            // Ende 20:00
            int BeginHours = app.getBegin().getHours(); // holt hh Beginn
                                                        // Schleifen-App.
            int BeginMin = app.getBegin().getMinutes(); // holt MM Beginn
                                                        // Schleifen-App.
                                                        //
            int EndHours = app.getEnd().getHours(); // holt hh Ende
                                                    // Schleifen-App.
            int EndMin = app.getEnd().getMinutes(); // holt MM Ende
                                                    // Schleifen-App.
                                                    //
            if (BeginHours > CatAHours || (BeginHours == CatAHours && BeginMin >= CatAMin)) {// falls
                                                                                             // Anfang
                                                                                             // SchlApp
                                                                                             // >=
                                                                                             // Anfang
                                                                                             // Cat
                if (BeginHours == CatEHours && BeginMin < CatEMin) // falls (hh
                                                                   // Anfang
                                                                   // SchlApp ==
                                                                   // hh Ende
                                                                   // SchlApp)
                                                                   // && (Begin
                                                                   // SchlApp <
                                                                   // MM Begin
                                                                   // SchlApp)
                                                                   // d.h. wenn
                                                                   // das
                                                                   // SchlApp
                                                                   // vor Ende
                                                                   // Cat
                                                                   // anfängt
                    sorted.add(app);
                if (BeginHours < CatEHours) // falls hh Beginn SchlApp < hh Ende
                                            // Cat
                    sorted.add(app);
            }
        }

        // zu diesem Punkt befinden sich in der ArrayList sorted alle Apointments des
        // reschedule-Zeitraums, die nach Anfang Cat und und min. 1 Minute vor
        // Ende Cat beginnen
    }
    
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
    von = sdf.parse("25.02.2013");
    bis = sdf.parse("02.03.2013");
    
    
    
    
    
    
    
    int dayOfMonthBeginSpan = von.getDay(); // 0=So      Anfangstag erlaubter Zeitraum
    int monthBeginSpan = von.getMonth(); // 0=Jan        Anfangsmonat erlaubter Zeitraum
    
    int dayOfMonthEndSpan = bis.getDay(); //             Endtag erlaubter Zeitaum
    int monthEndSpan = bis.getMonth();//                 Endmonat erlaubter Zeitraum
    
    
    
    
    
}

public int getTimeBetweenAppointments(Appointment appointment1, Appointment appointment2) {
    int gesamtDauer = 0;
    Date endeAppointment1 = appointment1.getEnd();
    Date beginnAppointment2 = appointment2.getBegin();
    long a = endeAppointment1.getTime();
    long b = beginnAppointment2.getTime();
    if (b > a) {
        long diff = beginnAppointment2.getTime()
                - endeAppointment1.getTime();
        double diffMinutes = diff / (60 * 1000);
        double diffHours = diffMinutes / 60;
        double diffSeconds = diffMinutes * 60;

        int h = (int) diffHours;
        double x = diffHours - h;
        double y = x * 60;
        int m = (int) y;
        int hInMinuten = h * 60;
        gesamtDauer = hInMinuten + m;
    }
    return gesamtDauer;
}

public int getLength(Appointment a) {
    int gesamtDauerMiApp = 0;
    Date beginnAppointment = a.getBegin();
    Date endeAppointment = a.getEnd();
    long an = beginnAppointment.getTime();
    long b = endeAppointment.getTime();
    if (b > an) {
        long diff = beginnAppointment.getTime()
                - beginnAppointment.getTime();
        double diffMinutes = diff / (60 * 1000);
        double diffHours = diffMinutes / 60;
        double diffSeconds = diffMinutes * 60;

        int h = (int) diffHours;
        double x = diffHours - h;
        double y = x * 60;
        int m = (int) y;
        int hInMinuten = h * 60;
        gesamtDauerMiApp = hInMinuten + m;
    }
    return gesamtDauerMiApp;
}
}
**/

